//
//  DEWMyGroupChatController.m
//  OtherDemo
//
//  Created by lianjie on 2018/7/25.
//  Copyright © 2018年 lianjie. All rights reserved.
//

#import "DEWMyGroupChatController.h"
#import "DEWGroupChatTableCell.h"

@interface DEWMyGroupChatController ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>
@property (nonatomic ,strong) UITableView *table;

///群聊列表
@property (nonatomic ,strong) NSMutableArray *chatList;
///搜索框
@property (nonatomic ,strong) UIView *searchView;
@property (nonatomic ,strong) UISearchBar *searchBar;
@end

@implementation DEWMyGroupChatController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的群聊";
    
    [self loadData];
    [self createView];
}

#pragma mark ----requestData
-(void)loadData
{
    weakSelf(weakSelf);
    
    NSMutableDictionary *pramseDic = [NSMutableDictionary dictionary];
    DELoginModel *model = [HHUserManager getUserInfo];
    [pramseDic setObject:model.userId forKey:@"userId"];
    [pramseDic setObject:model.tokenId forKey:@"tokenId"];
    
    NSString *url = [KSService myGroupChatListAPI];
    [KSHTTPSTool post:url params:pramseDic success:^(id responseObject) {
        if (DERetCode) {
            weakSelf.chatList = responseObject[@"data"];
            [weakSelf.table reloadData];
        }else{
            [[AlertHelper sharedManager]alertWithMessage:[NSString stringWithFormat:@"%@",responseObject[@"retMsg"]]];
        }
    } failure:^(NSError *error) {
        [weakSelf showHint:@"系统异常，稍后再试！"];
    }];
    
}
#pragma mark ---initView
-(void)createView
{
    [self setupSearchView];
    [self.view addSubview:self.table];
    
    
}
///初始化搜索框
- (void)setupSearchView
{
    _searchView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, DEWScreenWidth, 43.0f)];
    _searchView.backgroundColor = UIColorFromRGB(0xF1F1F1);
    [self.view addSubview:_searchView];
    
    _searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(5, 8, DEWScreenWidth-10.0f, _searchView.height-16.0f)];
    
    _searchBar.backgroundColor = WhiteColor;
    _searchBar.layer.cornerRadius = 4.0f;
    _searchBar.layer.masksToBounds = YES;
    _searchBar.delegate = self;
    _searchBar.placeholder = @"搜索群名称";
    [_searchView addSubview:_searchBar];
    
    
}
#pragma mark ---tableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.chatList.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DEWGroupChatTableCell *cell = [[DEWGroupChatTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    
    [cell createGroupCellWithInfo:self.chatList[indexPath.row] andStyle:DEWGroupCellStyleDefault];
    
    return cell;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 75;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.001f;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.001f;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    
}

#pragma mark --lazy
-(UITableView *)table
{
    if (!_table) {
        _table = [[UITableView alloc]initWithFrame:CGRectMake(0, _searchView.bottom, DEWScreenWidth, DEWScreenHeight-(NAVH)-self.searchView.height) style:UITableViewStyleGrouped];
        
        _table.delegate = self;
        
        _table.dataSource = self;
        _table.enablePlaceHolderView = YES;
        
        _table.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _table;
    
}
-(NSMutableArray *)chatList
{
    if (!_chatList) {
        _chatList = [NSMutableArray array];
    }
    return _chatList;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
